package com.ror.java.se.collection.set;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Demo01Set {
    public static void main(String[] args) {
        Set<String> s = new HashSet<>();

        //元素不重复
        System.out.println("=========元素不重复");
        boolean r1 = s.add("abc"); //true
        boolean r2 = s.add("abc"); //false
        System.out.println(r1);
        System.out.println(r2);
        System.out.println(s);

        //迭代器遍历
        System.out.println("=========迭代器遍历");
        s.add("123");
        s.add("你好");
        Iterator<String> it = s.iterator();
        while (it.hasNext()) {
            String str = it.next();
            System.out.println(str);
        }

        //增强for遍历
        System.out.println("=========增强for遍历");
        for (String str : s) {
            System.out.println(str);
        }

        //lambda表达式（匿名内部类实现Consumer接口）遍历
        System.out.println("=========lambda表达式遍历");
//        s.forEach(new Consumer<String>() {
//            @Override
//            public void accept(String str) {
//                System.out.println(str);
//            }
//        });
        //lambda表达式的简化写法
        s.forEach(str -> System.out.println(str));

    }

}
